# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Leonardo Arena <larena@alpinelinux.org>
pkgname=lxd
pkgver=5.0.3
pkgrel=1
pkgdesc="A container hypervisor and a new user experience for LXC - 'LTS' release channel"
url="https://ubuntu.com/lxd"
arch="all"
license="Apache-2.0"
depends="acl
	attr
	ca-certificates
	cgmanager
	dbus
	dnsmasq
	lxc
	iproute2
	iptables
	netcat-openbsd
	rsync
	squashfs-tools
	shadow-uidmap
	tar
	xz
	"
makedepends="acl-dev
	autoconf
	automake
	dqlite-dev
	eudev-dev
	gettext-dev
	go
	intltool
	libcap-dev
	libtool
	libuv-dev
	linux-headers
	lxc-dev
	lz4-dev
	raft-dev
	sqlite-dev
	tcl-dev
	"
subpackages="$pkgname-client
	$pkgname-scripts:scripts
	$pkgname-bash-completion:bashcomp:noarch
	$pkgname-openrc
	$pkgname-vm:vm:noarch
	"
provides=$pkgname-lts=$pkgver-r$pkgrel
install="$pkgname.pre-install"
options="!check" # FIXME: several tests failing
source="$pkgname-$pkgver-2.tar.gz::https://github.com/canonical/lxd/archive/refs/tags/lxd-$pkgver.tar.gz
	$pkgname.confd
	$pkgname.initd

	0002-bump-base-vm-filesystem-volume-to-500mib.patch
	0003-statically-build-lxd-agent-and-lxd-migrate.patch
	"
builddir="$srcdir/lxd-lxd-$pkgver"

_tools="lxc fuidshift lxc-to-lxd lxd-benchmark lxd-agent lxd-migrate"
_project="github.com/lxc/lxd"

export GOFLAGS="$GOFLAGS -tags=libsqlite3"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

prepare() {
	default_prepare
	go mod download
}

build() {
	export CGO_CFLAGS="-I/usr/include/raft -I/usr/include/dqlite"
	export CGO_LDFLAGS="$LDFLAGS -lintl"
	export CGO_LDFLAGS_ALLOW="(-Wl,-wrap,pthread_create)|(-Wl,-z,now)"

	mkdir bin
	for tool in lxd $_tools; do
		if [ "$tool" = "lxd-agent" ] || [ "$tool" = "lxd-migrate" ]; then
			make GOPATH="$builddir" $tool
		else
			go build -v -o bin/$tool ./$tool
		fi
	done
}

package() {
	install -Dm755 bin/lxd "$pkgdir"/usr/sbin/lxd

	for tool in $_tools; do
		install -Dm755 bin/$tool "$pkgdir"/usr/bin/$tool
	done

	install -Dm755 "$srcdir"/$pkgname.initd \
		"$pkgdir"/etc/init.d/lxd
	install -Dm644 "$srcdir"/$pkgname.confd \
		"$pkgdir"/etc/conf.d/lxd

	install -Dm644 scripts/bash/lxd-client \
		"$pkgdir"/usr/share/bash-completion/completions/lxd-client

	install -Dm755 scripts/empty-lxd.sh \
		"$pkgdir"/usr/bin/empty-lxd.sh
}

client() {
	pkgdesc="LXD CLI client"
	amove usr/bin/lxc
}

scripts() {
	pkgdesc="LXD scripts"
	depends="$pkgname py3-lxc"
	provides=$pkgname-lts-scripts=$pkgver-r$pkgrel

	amove usr/bin
}

vm() {
	pkgdesc="Install packages required to run VMs under LXD"
	depends="qemu-system-x86_64
		qemu-chardev-spice
		qemu-hw-usb-redirect
		qemu-hw-display-virtio-vga
		qemu-img
		qemu-ui-spice-core
		lxd-scripts
		ovmf
		sgdisk
		util-linux-misc
		virtiofsd
		"
	install -d "$subpkgdir"
}

openrc() {
	provides=$pkgname-lts-openrc=$pkgver-r$pkgrel
	default_openrc
}

check() {
	LXD_OFFLINE=true make check
}

sha512sums="
cbea9eb139b14e9dcfa585855c04f73246d6465ee0eeb9be71bc5497c476f4b1da57592665a5ca5c254e11e87397aabcf1666a13a5d90c83d16cd71c36b02fb7  lxd-5.0.3-2.tar.gz
1a88a60b09e0beccfa90357539d88a143e4e2099a2548dcc9109c374db045e6e8104af84c31151233961a15f505d3bbf1d5c6983637625663fb415d3c768e4ef  lxd.confd
3085a418f7260909e890cdda35b3dcc6bb1cb9c6268d507df0f7d00968cf185d185bea606b2514de8dc122581d485017f1b97758395e23fd4f53bab18c0cadf3  lxd.initd
44a47769f21bbd6260c0ca04145b57c367338adb2f41ee8149c9e6202e40b981a4278d996b78ec57e1b77de214337c0ea567e5266f74b02b1aa32ad309e7ab59  0002-bump-base-vm-filesystem-volume-to-500mib.patch
bd4c63e4ce70490b6dd510ab6d614f76bd6b6eabeeef18066fbc22fc6aaf9c35153da4b3bd716bd89f0ab6f8b6593c411c3d674934ce18acbb9f18c2abc834c8  0003-statically-build-lxd-agent-and-lxd-migrate.patch
"
